

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>scitools.PrmDictBase &mdash; SciTools 0.8.3 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.8.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="SciTools 0.8.3 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SciTools 0.8.3 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for scitools.PrmDictBase</h1><div class="highlight"><pre>
<span class="c">#!/usr/bin/env python</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Module for managing parameters.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">re</span><span class="o">,</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span>

<div class="viewcode-block" id="message"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.message">[docs]</a><span class="k">def</span> <span class="nf">message</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;DEBUG&#39;</span><span class="p">,</span> <span class="s">&#39;0&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s">&#39;1&#39;</span><span class="p">:</span>
        <span class="k">print</span> <span class="n">m</span>
    
</div>
<div class="viewcode-block" id="PrmDictBase"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase">[docs]</a><span class="k">class</span> <span class="nc">PrmDictBase</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Base class for managing parameters stored in dictionaries.</span>
<span class="sd">Typical use includes data or solver classes for solving physical</span>
<span class="sd">problems numerically. One may then choose to hold all physical</span>
<span class="sd">parameters in a dictionary physical_prm, containing</span>
<span class="sd">(parameter name, value) pairs, and all numerical parameters in</span>
<span class="sd">a dictionary numerical_prm. The physical_prm and numerical_prm</span>
<span class="sd">dictionaries can then be defined in a subclass of PrmDictBase</span>
<span class="sd">and managed by PrmDictBase. The management includes several</span>
<span class="sd">convenient features:</span>

<span class="sd"> - keeping all input data in one place</span>
<span class="sd"> - setting of one or more parameters where the type of the value</span>
<span class="sd">   must match the type of the previous (initial) value</span>
<span class="sd"> - pretty print of all defined parameters</span>
<span class="sd"> - copying parameters from dictionaries to, e.g., local variables</span>
<span class="sd">   and back again, or to local namespaces and back again</span>
<span class="sd"> - easy transition from parameter dictionaries to more sophisticated</span>
<span class="sd">   handling of input data, e.g., class scitools.ParameterInterface</span>
<span class="sd">   (GUI, CGI, command-line args)</span>

<span class="sd">The subclass typically defines the dictionaries, say</span>
<span class="sd">self.physical_prm and self.numerical_prm. Then these are</span>
<span class="sd">appended to the inherited self._prm_list list to be registered.</span>
<span class="sd">All members of this list are dictionaries that will not accept</span>
<span class="sd">new keys (i.e., all parameters must be defined prior to registering</span>
<span class="sd">them in self._prm_list). With this list one has a collection of all</span>
<span class="sd">parameter dictionaries in the application.</span>

<span class="sd">self._type_check[prm] is defined if we want to type check</span>
<span class="sd">a parameter prm.</span>
<span class="sd">if self._type_check[prm] is True (or False), prm must either</span>
<span class="sd">be None, of the same type as the previously registered</span>
<span class="sd">value of prm, or any number (float, int, complex) if</span>
<span class="sd">the previous value prm was any number. Instead of a boolean</span>
<span class="sd">value, self._type_check[prm] may hold a tuple of class types</span>
<span class="sd">(to be used in isinstance checks), or a function which takes</span>
<span class="sd">the value as argument and returns True if the that value is</span>
<span class="sd">of the right type (otherwise False).</span>


<span class="sd">In addition to the parameter dictionaries with fixed keys, class</span>
<span class="sd">PrmDictBase also holds a self.user_prm, which is a dictionary</span>
<span class="sd">of &quot;meta data&quot;, i.e., an arbitrary set of keys and values that</span>
<span class="sd">can arbitrarily extended anywhere. If self.user_prm is None,</span>
<span class="sd">no such meta data can exists (implying that only parameters</span>
<span class="sd">registered in the dictionaries in self._prm_list are allowed - the</span>
<span class="sd">programmer of subclasses can of course extend these parameter</span>
<span class="sd">sets whenever desired; disallowing a parameter name is only a</span>
<span class="sd">feature of the set function for setting the value of a (registered)</span>
<span class="sd">parameter).</span>

<span class="sd">Here is an example::</span>

<span class="sd">    from scitools.PrmDictBase import PrmDictBase</span>

<span class="sd">    class SomeSolver(PrmDictBase):</span>
<span class="sd">        def __init__(self, **kwargs):</span>
<span class="sd">            PrmDictBase.__init__(self)</span>
<span class="sd">            # register parameters in dictionaries:</span>
<span class="sd">            self.physical_prm = {&#39;density&#39;: 1.0, &#39;Cp&#39;: 1.0,</span>
<span class="sd">                                       &#39;k&#39;: 1.0, &#39;L&#39;: 1.0}</span>
<span class="sd">            self.numerical_prm =  {&#39;n&#39;: 10, &#39;dt&#39;: 0.1, &#39;tstop&#39;: 3}</span>

<span class="sd">            # attach dictionaries to base class list (required):</span>
<span class="sd">            self._prm_list = [self.physical_prm, self.numerical_prm]</span>

<span class="sd">            # specify parameters to be type checked when set:</span>
<span class="sd">            self._type_check.update({&#39;n&#39;: True, &#39;dt&#39;: (float,),</span>
<span class="sd">                  &#39;k&#39;: lambda k: isinstance(int,float) and k&gt;0})</span>

<span class="sd">            # disallow arbitrary meta data</span>
<span class="sd">            self.user_prm = None # set to {} if meta data are allowed</span>

<span class="sd">            # initialize parameters according to keyword arguments:</span>
<span class="sd">            self.set(**kwargs)</span>


<span class="sd">        def _update(self):</span>
<span class="sd">            # dt depends on n, L, k; update dt in case the three</span>
<span class="sd">            # others parameters have been changed</span>
<span class="sd">            # (in general this method is used to check consistency</span>
<span class="sd">            # between parameters and perform updates if necessary)</span>
<span class="sd">            n = self.numerical_prm[&#39;n&#39;]</span>
<span class="sd">            L = self.physical_prm[&#39;L&#39;]</span>
<span class="sd">            k = self.physical_prm[&#39;k&#39;]</span>

<span class="sd">            self.u = zeros(n+1, Float)</span>
<span class="sd">            h = L/float(n)</span>
<span class="sd">            dt_limit = h**2/(2*k)</span>
<span class="sd">            if self.numerical_prm[&#39;dt&#39;] &gt; dt_limit:</span>
<span class="sd">                self.numerical_prm[&#39;dt&#39;] = dt_limit</span>

<span class="sd">        def compute1(self):</span>
<span class="sd">            # compute something</span>
<span class="sd">            return self.physical_prm[&#39;k&#39;]/self.physical_prm[&#39;Cp&#39;]</span>

<span class="sd">        def compute2(self):</span>
<span class="sd">            # turn numerical parameters into local variables:</span>
<span class="sd">            exec self.dicts2variables(self._prm_list)</span>
<span class="sd">            # or exec self.dicts2variables(self.numerical_prm)  # selected prms</span>

<span class="sd">            # now we have local variables n, dt, tstop, density, Cp, k, L</span>
<span class="sd">            # that we can compute with, say</span>

<span class="sd">            Q = k/Cp</span>
<span class="sd">            dt = 0.9*dt</span>

<span class="sd">            # if some of the local variables are changed, say dt, they must</span>
<span class="sd">            # be inserted back into the parameter dictionaries:</span>
<span class="sd">            self.variables2dicts(self.numerical_prm, dt=dt)    </span>

<span class="sd">    &quot;&quot;&quot;</span>
    
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># dicts whose keys are fixed (non-extensible):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prm_list</span> <span class="o">=</span> <span class="p">[]</span>     <span class="c"># fill in subclass</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_prm</span> <span class="o">=</span> <span class="bp">None</span>    <span class="c"># user&#39;s meta data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span> <span class="o">=</span> <span class="p">{}</span>   <span class="c"># fill in subclass</span>

    <span class="k">def</span> <span class="nf">_prm_dict_names</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the name of all self.*_prm dictionaries.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">attr</span> <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span> <span class="k">if</span> \
                <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">r&#39;^[^_].*_prm$&#39;</span><span class="p">,</span> <span class="n">attr</span><span class="p">)]</span>
                
<div class="viewcode-block" id="PrmDictBase.usage"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.usage">[docs]</a>    <span class="k">def</span> <span class="nf">usage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Print the name of all parameters that can be set.&quot;&quot;&quot;</span>
        <span class="n">prm_dict_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prm_dict_names</span><span class="p">()</span>
        <span class="n">prm_names</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">prm_dict_names</span><span class="p">:</span>
            <span class="n">d</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                <span class="n">k</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
                <span class="n">k</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">:</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span><span class="n">b</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
                <span class="n">prm_names</span> <span class="o">+=</span> <span class="n">k</span>
        <span class="k">print</span> <span class="s">&#39;registered parameters:</span><span class="se">\n</span><span class="s">&#39;</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">prm_names</span><span class="p">:</span>
            <span class="k">print</span> <span class="n">i</span>
        <span class="c"># alternative (sort all in one bunch):</span>
        <span class="c"># names = []</span>
        <span class="c"># for d in self._prm_list:</span>
        <span class="c">#     names += d.keys()</span>
        <span class="c"># names.sort</span>
        <span class="c"># print names</span>
</div>
<div class="viewcode-block" id="PrmDictBase.dump"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.dump">[docs]</a>    <span class="k">def</span> <span class="nf">dump</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Dump all parameters and their values.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prm_list</span><span class="p">:</span>
            <span class="n">keys</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
            <span class="n">keys</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">:</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span><span class="n">b</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
            <span class="k">for</span> <span class="n">prm</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
                <span class="k">print</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="n">prm</span><span class="p">])</span>
</div>
<div class="viewcode-block" id="PrmDictBase.set"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.set">[docs]</a>    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Set kwargs data in parameter dictionaries.&quot;&quot;&quot;</span>
        <span class="c"># print usage message if no arguments:</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">usage</span><span class="p">()</span>
            <span class="k">return</span>
        
        <span class="k">for</span> <span class="n">prm</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="n">_set</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prm_list</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;self._prm_list is wrong (empty)&#39;</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">set_in_dict</span><span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">prm</span><span class="p">],</span> <span class="n">d</span><span class="p">):</span>
                        <span class="n">_set</span> <span class="o">=</span> <span class="bp">True</span>
                        <span class="k">break</span>
                <span class="k">except</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="n">msg</span><span class="p">:</span>
                    <span class="k">print</span> <span class="n">msg</span>
                    <span class="c">#break</span>
                    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c"># type error is fatal</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">_set</span><span class="p">:</span>   <span class="c"># maybe set prm as meta data?</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">user_prm</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                    <span class="c"># not a registered parameter:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">user_prm</span><span class="p">[</span><span class="n">prm</span><span class="p">]</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">prm</span><span class="p">]</span>
                    <span class="n">message</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s"> assigned in self.user_prm&#39;</span> <span class="o">%</span> \
                            <span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">prm</span><span class="p">]))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">NameError</span><span class="p">(</span><span class="s">&#39;parameter &quot;</span><span class="si">%s</span><span class="s">&quot; not registered&#39;</span> <span class="o">%</span> <span class="n">prm</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="PrmDictBase.set_in_dict"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.set_in_dict">[docs]</a>    <span class="k">def</span> <span class="nf">set_in_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prm</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set d[prm]=value, but check if prm is registered in class</span>
<span class="sd">        dictionaries, if the type is acceptable, etc.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">can_set</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="c"># check that prm is a registered key</span>
        <span class="k">if</span> <span class="n">prm</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">prm</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">:</span>
                <span class="c"># prm should be type-checked</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">],</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span><span class="nb">float</span><span class="p">)):</span>
                    <span class="c"># (bool is subclass of int)</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">]:</span>
                        <span class="c"># type check against prev. value or None:</span>
                        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="n">prm</span><span class="p">]),</span> <span class="bp">None</span><span class="p">)):</span>
                            <span class="n">can_set</span> <span class="o">=</span> <span class="bp">True</span>
                        <span class="c"># allow mixing int, float, complex:</span>
                        <span class="k">elif</span> <span class="n">operator</span><span class="o">.</span><span class="n">isNumberType</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="ow">and</span>\
                                 <span class="n">operator</span><span class="o">.</span><span class="n">isNumberType</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="n">prm</span><span class="p">]):</span>
                            <span class="n">can_set</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">],</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span><span class="nb">list</span><span class="p">,</span><span class="nb">type</span><span class="p">)):</span>
                    <span class="c"># self._type_check[prm] holds either the type or</span>
                    <span class="c"># a tuple/list of types; test against them</span>
                    <span class="c">#print &#39;testing %s=%s against type %s&#39; % (prm,value,self._type_check[prm])</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">]):</span>
                        <span class="n">can_set</span> <span class="o">=</span> <span class="bp">True</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n\n</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s"> has type </span><span class="si">%s</span><span class="s">, not </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> \
                                        <span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">prm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">],</span>
                                         <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)))</span>

                <span class="k">elif</span> <span class="nb">callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">]):</span>
                    <span class="n">can_set</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">](</span><span class="n">value</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&#39;self._type_check[&quot;</span><span class="si">%s</span><span class="s">&quot;] has an &#39;</span>\
                                    <span class="s">&#39;illegal value </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> \
                                    <span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type_check</span><span class="p">[</span><span class="n">prm</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">can_set</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">message</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> is not registered in</span><span class="se">\n</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">can_set</span><span class="p">:</span>
            <span class="n">d</span><span class="p">[</span><span class="n">prm</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
            <span class="n">message</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s"> is assigned&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">prm</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="bp">False</span>
        
</div>
    <span class="k">def</span> <span class="nf">_update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check data consistency and make updates.&quot;&quot;&quot;</span>
        <span class="c"># to be implemented in subclasses</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="PrmDictBase.get"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.get">[docs]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_solver_prm</span><span class="p">[</span><span class="n">prm</span><span class="p">]</span> \
                <span class="k">for</span> <span class="n">prm</span> <span class="ow">in</span> <span class="n">kwargs</span> <span class="k">if</span> <span class="n">prm</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_solver_prm</span><span class="p">]</span>
                </div>
<div class="viewcode-block" id="PrmDictBase.properties"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.properties">[docs]</a>    <span class="k">def</span> <span class="nf">properties</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">global_namespace</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Make properties out of local dictionaries.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">ds</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prm_dict_names</span><span class="p">():</span>
            <span class="n">d</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="s">&#39;self.&#39;</span> <span class="o">+</span> <span class="n">ds</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">prm</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span> <span class="c"># or for prm in self.__dict__[ds]</span>
                <span class="c"># properties cannot have whitespace:</span>
                <span class="n">prm</span> <span class="o">=</span> <span class="n">prm</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span> <span class="s">&#39;_&#39;</span><span class="p">)</span>
                <span class="n">cmd</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s"> = property(fget=&#39;</span>\
                      <span class="s">&#39;lambda self: self.</span><span class="si">%s</span><span class="s">[&quot;</span><span class="si">%s</span><span class="s">&quot;], </span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> \
                      <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">prm</span><span class="p">,</span> <span class="n">ds</span><span class="p">,</span> <span class="n">prm</span><span class="p">,</span>
                       <span class="s">&#39; doc=&quot;read-only property&quot;&#39;</span><span class="p">)</span>
                <span class="k">print</span> <span class="n">cmd</span>
                <span class="k">exec</span> <span class="n">cmd</span> <span class="ow">in</span> <span class="n">global_namespace</span><span class="p">,</span> <span class="nb">locals</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="PrmDictBase.dicts2namespace"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.dicts2namespace">[docs]</a>    <span class="k">def</span> <span class="nf">dicts2namespace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">dicts</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Make namespace variables out of dict items.</span>
<span class="sd">        That is, for all dicts, insert all (key,value) pairs in</span>
<span class="sd">        the namespace dict.</span>
<span class="sd">        namespace is a dictionary, dicts is a list of dictionaries.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># can be tuned in subclasses</span>

        <span class="c"># allow dicts to be a single dictionary:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dicts</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span><span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">dicts</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dicts</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">overwrite</span><span class="p">:</span>
                <span class="n">namespace</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">namespace</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">overwrite</span><span class="p">:</span>
                        <span class="k">print</span> <span class="s">&#39;cannot overwrite </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">key</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">namespace</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="PrmDictBase.dicts2namespace2"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.dicts2namespace2">[docs]</a>    <span class="k">def</span> <span class="nf">dicts2namespace2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">dicts</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;As dicts2namespace2, but use exec.&quot;&quot;&quot;</span>
        <span class="c"># can be tuned in subclasses</span>

        <span class="c"># allow dicts to be a single dictionary:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dicts</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span><span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">dicts</span><span class="p">]</span>
            
        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dicts</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
                <span class="k">exec</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="nb">repr</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]))</span> <span class="ow">in</span> <span class="nb">globals</span><span class="p">(),</span> <span class="n">namespace</span>
</div>
<div class="viewcode-block" id="PrmDictBase.namespace2dicts"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.namespace2dicts">[docs]</a>    <span class="k">def</span> <span class="nf">namespace2dicts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">dicts</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update dicts from variables in a namespace.</span>
<span class="sd">        That is, for all keys in namespace, insert (key,value) pair</span>
<span class="sd">        in the dict in dicts that has the same key registered.</span>
<span class="sd">        namespace is a dictionary, dicts is a list of dictionaries.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># allow dicts to be a single dictionary:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dicts</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span><span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">dicts</span><span class="p">]</span>

        <span class="n">keys</span> <span class="o">=</span> <span class="p">[]</span>    <span class="c"># all keys in namespace that are keys in dicts</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">namespace</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dicts</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
                    <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">namespace</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>  <span class="c"># update value</span>
                    <span class="n">keys</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>         <span class="c"># mark for delete</span>
        <span class="c"># clean up what we made in self.dicts2namespace:</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">namespace</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="PrmDictBase.dicts2variables"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.dicts2variables">[docs]</a>    <span class="k">def</span> <span class="nf">dicts2variables</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dicts</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Make Python code string that defines local variables from</span>
<span class="sd">        all parameters in dicts (list of dictionaries of parameters).</span>
<span class="sd">        For example, if dicts[1] has a key n with value 1.0, the</span>
<span class="sd">        statement &#39;n=1.0&#39; will be included in the returned string.</span>
<span class="sd">        The calling code will typically exec this returned string</span>
<span class="sd">        to make local variables (short hands) from parameters stored</span>
<span class="sd">        in dictionaries. (Note that such local variables are read-only,</span>
<span class="sd">        changing their values will not be reflected in the dictionaries!).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># allow dicts to be a single dictionary:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dicts</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span><span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">dicts</span><span class="p">]</span>

        <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dicts</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
                <span class="n">s</span> <span class="o">+=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="n">name</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">s</span>
</div>
<div class="viewcode-block" id="PrmDictBase.variables2dicts"><a class="viewcode-back" href="../../PrmDictBase.html#scitools.PrmDictBase.PrmDictBase.variables2dicts">[docs]</a>    <span class="k">def</span> <span class="nf">variables2dicts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dicts</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Insert the name=value keyword arguments in variables into</span>
<span class="sd">        the dictionaries in dicts (list of dictionaries).</span>
<span class="sd">        This is the inverse of the dicts2variables function.</span>

<span class="sd">        Usage:</span>
<span class="sd">        exec self.dicts2variables(self.numerical_prm)</span>
<span class="sd">        # work with read-only n, dt, tstop</span>
<span class="sd">        ...</span>
<span class="sd">        # update (in case n, dt, tstop was changed):</span>
<span class="sd">        self.variables2dicts(self.numerical_prm, n=n, dt=dt, tstop=tstop)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">variables</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dicts</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
                    <span class="n">d</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">variables</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
    

<span class="c"># initial tests are found in src/py/examples/classdicts.py</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/scitools_logo.jpg" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SciTools 0.8.3 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, H. P. Langtangen, J. Ring, ++.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>